/**
 *
 */
package com.iknight.mgr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.mgr.api.PermissionService;
import com.iknight.mgr.mapper.PermissionMapper;
import com.iknight.mgr.mapper.RoleMapper;
import com.iknight.mgr.model.PermissionModel;
import com.iknight.mgr.model.RoleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Jeryzym
 */
@Service
public class PermissionServiceImpl implements PermissionService<PermissionModel, String> {
    @Autowired
    private PermissionMapper<PermissionModel, String> permissionMapper;
    @Autowired
    private RoleMapper<RoleModel, String> roleMapper;

    public void save(PermissionModel record) {
        // TODO Auto-generated method stub
        if (permissionMapper.selectOne(record.getPerm_id()) != null) {
            permissionMapper.update(record);
        } else {
            permissionMapper.insert(record);
        }
    }

    @Transactional
    public void delete(String pkey) {
        // TODO Auto-generated method stub
        roleMapper.releasePermFromRole(pkey);
        permissionMapper.delete(pkey);
    }

    public PermissionModel selectOne(String pkey) {
        // TODO Auto-generated method stub
        return permissionMapper.selectOne(pkey);
    }

    public PageInfo<PermissionModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageStart, pageSize, true);
        List<PermissionModel> result = permissionMapper.selectByCond(params);
        PageInfo<PermissionModel> page = new PageInfo<PermissionModel>(result);
        return page;
    }

    @Override
    public PermissionModel getRecordByCode(String permCode) {
        // TODO Auto-generated method stub
        return permissionMapper.getRecordByCode(permCode);
    }

}
